package com.lan.iot;

import androidx.appcompat.app.AppCompatActivity;
import androidx.localbroadcastmanager.content.LocalBroadcastManager;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Switch;
import android.widget.Toast;

public class Fenwei extends AppCompatActivity {
    int roomid;
    Switch sw;
    Dialog dlg;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().requestFeature(Window.FEATURE_NO_TITLE);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {

            Window window = getWindow();

            window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS

                    | WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);

            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN

                    | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION

                    | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);

            window.setStatusBarColor(Color.TRANSPARENT);

            window.setNavigationBarColor(Color.TRANSPARENT);
        }
        getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_fenwei);
        Button back = findViewById(R.id.back);
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
         sw=findViewById(R.id.sw);
        Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        boolean isOpen = bundle.getBoolean("name", true);
        roomid=bundle.getInt("roomid");
        if (isOpen) sw.setChecked(true);
        moshi=findViewById(R.id.moshi);
        moshi.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Drawable dr = getResources().getDrawable(R.drawable.radius_border);
                 dlg = new AlertDialog.Builder(Fenwei.this).create();
                dlg.show();//显示对话框
                Window window = dlg.getWindow();//获取对话框窗口\
                window.setGravity(Gravity.BOTTOM);//设置对话框显示剧中的位置
                window.setContentView(R.layout.xct);//设置对话框布局
                window.clearFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);

                window.setBackgroundDrawable(dr);
                WindowManager.LayoutParams params = dlg.getWindow()
                        .getAttributes();
                Button gr1=window.findViewById(R.id.gr1);
                gr1.setOnClickListener(clickListener);
                Button gr2=window.findViewById(R.id.gr2);
                gr2.setOnClickListener(clickListener);
                Button gr3=window.findViewById(R.id.gr3);
                gr3.setOnClickListener(clickListener);
                Button gr4=window.findViewById(R.id.gr4);
                gr4.setOnClickListener(clickListener);
                Button gr5=window.findViewById(R.id.gr5);
                gr5.setOnClickListener(clickListener);
                Button gr6=window.findViewById(R.id.gr6);
                gr6.setOnClickListener(clickListener);
                Button gr7=window.findViewById(R.id.gr7);
                gr7.setOnClickListener(clickListener);
//                Display display = getWindowManager().getDefaultDisplay();
//                params.width = (int) (dpToPx(344));
//                        params.width=dpToPx(360);
//                params.dimAmount = 0.0f;
                dlg.getWindow().setAttributes(params);

            }
        });
    }
    int moid=1;
    View.OnClickListener clickListener=new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            switch (view.getId()){
                case R.id.gr1:moid=1;break;
                case R.id.gr2:moid=2;break;
                case R.id.gr3:moid=3;break;
                case R.id.gr4:moid=4;break;
                case R.id.gr5:moid=5;break;
                case R.id.gr6:moid=6;break;
                case R.id.gr7:moid=7;break;
            }
            moshi.setText("模式"+moid);
            dlg.cancel();
        }
    };
    Button moshi;
    @Override
    protected void onStop() {
        super.onStop();

        Intent intent2 = new Intent("android.intent.action.CART_BROADCAST_fenwei");
        intent2.putExtra("acdndata", sw.isChecked());
        intent2.putExtra("roomid", roomid);
        LocalBroadcastManager.getInstance(Fenwei.this).sendBroadcast(intent2);
        sendBroadcast(intent2);

    }
}
